//
//  fff.swift
//  FastSwiftSDK
//
//  Created by XX on 2024/1/18.
//
public class XXListenerManager {
    public static let shared = XXListenerManager()
    private var routeHandlers:Array<Dictionary<String,(String?) -> Void>> = []
    private var applicationHandlers:Array<Dictionary<String,(String?) -> Void>> = []
    
    private init() {}
    
    // route : 模块名称://方法名   例 "FastFBEvents://CheckOut"
    // handler  处理逻辑   string 参数
    // 注册路由规则
    public func registerRoute(_ route: String, handler: @escaping (String?) -> Void) {
        routeHandlers.append([route:handler])
    }
    
    // 处理路由请求
    public func handleRoute(_ route: String,_ parameters:String?) {
        var isHave = false
        for dic in self.routeHandlers{
            for(key,value) in dic{
                if key == route{
                    let handle:(String?) -> Void = value
                    handle(parameters)
                    isHave = true
                    break
                }
            }
        }
        if isHave == false{
            // 处理未匹配到路由规则的情况
//            FastLog.debug("路由未识别")
        }
    }
    
    // 删除
    public func remove(_ route: String){
        for dic in self.routeHandlers{
            self.routeHandlers.removeAll { $0.keys.contains(route) }
        }
    }
}
